package com.spsyry.pay.conteoller.api;

import com.alibaba.fastjson.JSON;
import com.spsyry.api.pay.PayApi;
import com.spsyry.api.vo.AddOrderVo;
import com.spsyry.common.core.domain.R;
import com.spsyry.pay.conteoller.PaymentController;
import com.spsyry.pay.domain.PaymentOrder;
import com.spsyry.pay.enums.PayOrderEnum;
import com.spsyry.pay.order.WeiXinUtil;
import com.spsyry.pay.service.IPaymentOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

@RestController
@Slf4j
public class PayApicontroller implements PayApi {

    @Autowired
    private PaymentController paymentController;
    @Autowired
    private IPaymentOrderService paymentOrderService;

    @Override
    public R createOrder(AddOrderVo addOrderVo) throws Exception {
        log.info("支付接口接收的参数add", JSON.toJSONString(addOrderVo));
        PaymentOrder paymentOrder = PaymentOrder.builder()
                .errorCode("A")
                .orderNo(UUID.randomUUID().toString())
                .notifyUrl("url")
                .errorMessage("1")
                .expireTime(new Date())
                .payAmount(new BigDecimal(12))
                .payMethod("payMethod")
                .userIp("ip")
                .notifyStatus(PayOrderEnum.REFUND.getDescription())
                .merchantOrderNo(addOrderVo.getOrderId())
                .payTime(new Date())
                .build();
        String random = RandomStringUtils.random(5);

        System.out.println(random);

        Integer save = paymentOrderService.insertPaymentOrder(paymentOrder);

        String payOrder = WeiXinUtil.payOrder(addOrderVo.getOrderId());
        System.out.println("水水水水水水水水"+payOrder);
        return R.ok(payOrder);
    }
}
